; filename: win_cd_boot.nsi
;
; Linux installer: Install linux boots from you hard disk, so that
;                  the system bios does not need to be changed.
;
;                  In order to achieve that, grub for DOS is installed
;                  into your hard disc and it is set up to boot from first CD.
;                  Then, boot.ini is changed to include grub on the boot process.
;                  After that, the system will be rebooted so that you can install
;                  linux. Next time you boot windows, the boot.ini will be restored.
;
;    Copyright (C) 2005  Jordi Massaguer Pla
;    Copyright (C) 2007 Krzysztof Lichota

;    This program is free software; you can redistribute it and/or modify
;    it under the terms of the GNU General Public License as published by
;    the Free Software Foundation; either version 2 of the License, or
;    (at your option) any later version.
;
;    This program is distributed in the hope that it will be useful,
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;    GNU General Public License for more details.
;
;    You should have received a copy of the GNU General Public License
;    along with this program; if not, write to the Free Software
;    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
;
; 
;--------------------------------
;Include Modern UI
 ; GetWindowsVersion
 ;
 ; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
 ; Updated by Joost Verburg
 ;
 ; Returns on top of stack
 ;
 ; Windows Version (95, 98, ME, NT x.x, 2000, XP, 2003)
 ; or
 ; '' (Unknown Windows Version)
 ;
 ; Usage:
 ;   Call GetWindowsVersion
 ;   Pop $R0
 ;   ; at this point $R0 is "NT 4.0" or whatnot
 
 Function GetWindowsVersion
 
   Push $R0
   Push $R1
 
   ClearErrors
 
   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion

   IfErrors 0 lbl_winnt
   
   ; we are not NT
   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber
 
   StrCpy $R1 $R0 1
   StrCmp $R1 '4' 0 lbl_error
 
   StrCpy $R1 $R0 3
 
   StrCmp $R1 '4.0' lbl_win32_95
   StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98
 
   lbl_win32_95:
     StrCpy $R0 '95'
   Goto lbl_done
 
   lbl_win32_98:
     StrCpy $R0 '98'
   Goto lbl_done
 
   lbl_win32_ME:
     StrCpy $R0 'ME'
   Goto lbl_done
 
   lbl_winnt:
 
   StrCpy $R1 $R0 1
 
   StrCmp $R1 '3' lbl_winnt_x
   StrCmp $R1 '4' lbl_winnt_x
 
   StrCpy $R1 $R0 3
 
   StrCmp $R1 '5.0' lbl_winnt_2000
   StrCmp $R1 '5.1' lbl_winnt_XP
   StrCmp $R1 '5.2' lbl_winnt_2003 lbl_error
 
   lbl_winnt_x:
     StrCpy $R0 "NT $R0" 6
   Goto lbl_done
 
   lbl_winnt_2000:
     Strcpy $R0 '2000'
   Goto lbl_done
 
   lbl_winnt_XP:
     Strcpy $R0 'XP'
   Goto lbl_done
 
   lbl_winnt_2003:
     Strcpy $R0 '2003'
   Goto lbl_done
 
   lbl_error:
     Strcpy $R0 ''
   lbl_done:
 
   Pop $R1
   Exch $R0
 
 FunctionEnd

  !include "MUI.nsh"

;--------------------------------
;General

  Name "Boot system from CD to install Linux"
  AllowRootDirInstall true
  OutFile "win_cd_boot.exe"
  Caption "Boot system from CD to install Linux"

  ;Default installation folder
  InstallDir "c:\"
  BGGradient ffffff ffffff

;  !define MUI_ICON "linux.ico"
;  !define MUI_UNICON "linux.ico"
;  !define MUI_HEADERIMAGE
;  !define MUI_HEADERIMAGE_BITMAP "logo.bmp"

;--------------------------------
;Interface Settings

  !define MUI_ABORTWARNING

;--------------------------------
;Pages

  !define MUI_WELCOMEPAGE_TITLE_3LINES 
  !insertmacro MUI_PAGE_WELCOME
  !insertmacro MUI_PAGE_LICENSE "license_en.txt"
  !insertmacro MUI_PAGE_INSTFILES
  !insertmacro MUI_PAGE_FINISH    
  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
  
;--------------------------------
;Languages
 
  !insertmacro MUI_LANGUAGE "English"

;--------------------------------
; splash

XPStyle on
Function .onInstFailed
    MessageBox MB_OK "Installation failed."
FunctionEnd

Function .onInit
	# the plugins dir is automatically deleted when the installer exits
	InitPluginsDir
;	File /oname=$PLUGINSDIR\logo.bmp "logo.bmp"
	splash::show 1000 $PLUGINSDIR\splash
	Pop $0 ; $0 has '1' if the user closed the splash screen early,
			; '0' if everything closed normally, and '-1' if some error occurred.
FunctionEnd

;--------------------------------

;Vars

var MenuLSTFile

;-------------------------------

;Sections

Section ""
  Call GetWindowsVersion
   Pop $R0
   StrCmp $R0 "2000" versionOK
   StrCmp $R0 "XP" versionOK
   StrCmp $R0 "2003" versionOK
   Abort "This installer works only in Windows 2000, XP or 2003"
versionOK:
  SetOutPath $INSTDIR
  ClearErrors
  FileOpen $MenuLSTFile c:\menu.lst w
  FileWrite $MenuLSTFile "timeout 2$\r$\n"
  FileWrite $MenuLSTFile "default 0$\r$\n"
  FileWrite $MenuLSTFile "title Boot system from CD to install Linux$\r$\n"
  FileWrite $MenuLSTFile "cdrom --init$\r$\n"
  FileWrite $MenuLSTFile "map --hook$\r$\n"
  FileWrite $MenuLSTFile "chainloader (cd0)$\r$\n"
  
  FileSeek $MenuLSTFile 0 END
  FileClose $MenuLSTFile
  IfErrors 0 +2
    Abort "Failed to write to menu.lst"
  ClearErrors
  File "grldr"
  IfErrors 0 +2
    Abort "Failed to copy grldr"
  ExecWait "attrib -h -s -r C:\boot.ini"
  IfErrors 0 +2
    Abort "Failed to change attributes of boot.ini"
  ; on some systems timeout is 0 and prevents choosing operating system at boot
  WriteINIStr c:\boot.ini "boot loader" "timeout" "30"
  WriteINIStr c:\boot.ini "operating systems" "c:\grldr" '"Boot system from CD to install Linux"'
  IfErrors 0 +2
    Abort "Failed to write to boot.ini"
  ExecWait "attrib +h +s +r C:\boot.ini"
  IfErrors 0 +2
    Abort "Failed to restore attributes of boot.ini"
  WriteUninstaller "$SMSTARTUP\instlux-uninst.exe"
  SetRebootFlag true
SectionEnd

;-------------------------------------------
; uninstall section

;Uninstaller

Section "Uninstall"

  Delete /REBOOTOK "C:\menu.lst"
  Delete /REBOOTOK "C:\grldr"
  ExecWait "attrib -h -s -r C:\boot.ini"
  ; we did not save timeout, so write 10 as compromise between 30 and 0
  WriteINIStr c:\boot.ini "boot loader" "timeout" "10"
  DeleteINIStr c:\boot.ini "operating systems" "c:\grldr"
  ExecWait "attrib +h +s +r C:\boot.ini"
  Delete /REBOOTOK "$SMSTARTUP\instlux-uninst.exe"
  
SectionEnd
